var vmNavigation = new Vue({
    el: '#main-block',
    data: function(){
        return {
            dialogImageUrl: '',
            dialogVisible: false,
            uploadBarList:[],
            imageUrl: '',
            type: 'default',
            data: [],
            addParamData:{},
            uploadLimit: 1,
            fileType: '.jpg,.jpeg,.gif,.png,.bmp,.svg,.PNG,.JPG',
            tmp: {
                id: '',
                label: '新增导航',
                deep: 2,
                type: 1,
                link: '#',
                icon: '',
                img: '',
                imgWidth: 30,
                imgHeight: 30,
                isUsable: 1,
                children: []
            },
            typeOptions: [
                {
                    value: 1,
                    label: '文字链接'
                },
                {
                    value: 2,
                    label: '上图下文'
                },
                {
                    value: 3,
                    label: '左图右文'
                }
            ],
            uploadUrl: '',
            uploadData:{
                path:''
            },
            bannerData:{
                "hrefs":'',
                "url": '',
                "name": '',
                "uid": '',

            },
            isNew:true,
            isShow:false,
            formLabelWidth:'150px'
        };
    },
    methods: {
        handleRemove(file, fileList) {
            console.log(file, fileList);
            var self = this;
            self.uploadBarList.forEach((item, index) => {
                if (item.uid === file.uid) {
                    delete self.uploadBarList[index]
                }
            })

            // self.uploadBarList = [];
            // fileList.forEach(function (item,index) {
            //     var barImg = {};
            //     if(item.response == undefined){
            //        barImg.url = item.url; 
            //     }else{
            //        barImg.url = item.response.url; 
            //     }
            //     barImg.name = item.name;
            //     self.uploadBarList.push(barImg);
            // });
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        uploadSuccessDeal: function (response, file, fileList) {
            var self = this;
            self.bannerData.url = file.response.url;
            self.bannerData.name = file.name;
            self.bannerData.uid = file.uid;

            // self.uploadBarList = [];
            // fileList.forEach(function (item,index) {
            //     var barImg = {};
            //     if(item.response == undefined){
            //        barImg.url = item.url; 
            //     }else{
            //        barImg.url = item.response.url; 
            //     }
            //     barImg.name = item.name;
            //     self.uploadBarList.push(barImg);
            // });
        },
        uptDicParam:function(){
            var self = this;
            var request = {
                type: self.type
            };
            var data = [];

            var service = {"paramName":"navBarImg","paramValue": Commonjs.jsonToString(this.uploadBarList),"paramType":"1"};
            var fn="addNavScrollBar";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, encodeURIComponent(service));
            Commonjs.ajaxSilence(weburl, params, true, function (response) {
                self.$message({
                    message: '轮播图替换成功',
                    type: 'success'
                });
            }, function () {
                self.$message.error("获取导航配置错误");
            });
        },
        getNavScrollData:function(){
            var self = this;
            var service = {};
            var fn="getParamByNameAndType";
            service.paramName = "navBarImg";
            service.paramType = "1";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn,service);//获取参数
            Commonjs.ajaxSilence(sysurl,params,true,function (data) {
                self.uploadBarList = data.data.paramValue;

            },function () {
                
            });
        },

        /**
         * 获取配置.
         */
        // loadConfig: function() {
        //     var self = this;
        //     var fn="navigationGet";
        //     var service = Commonjs.jsonToString(request);
        //     var params = Commonjs.getParams(fn, service);
        //     Commonjs.ajaxSilence(weburl, params, true, function (response) {
        //         self.data = response.data;
        //     }, function () {
        //         self.$message.error("获取导航配置错误");
        //     });
        // },
        addBanner:function(){
            var self = this;
            // this.$refs.uploader.clearFiles()
            self.isShow = true;
            self.bannerData={
                "hrefs":'',
                "url": '',
                "name": '',
                "uid": ''
            };

            // self.bannerData={};
        },
        saveBanner:function(){
            var self = this;
            if(self.isNew){
                self.uploadBarList.push(self.bannerData);
            }else{
                self.uploadBarList.forEach((item, index) => {
                    if (item.uid === self.bannerData.uid) {
                        self.uploadBarList[index].url = self.bannerData.url;
                        self.uploadBarList[index].hrefs = self.bannerData.hrefs;
                        // delete self.uploadBarList[index];
                    }
                })
            }
            self.isNew = true;
            self.updateBanner();
            // var service = {"paramName":"navBarImg","paramValue": Commonjs.jsonToString(this.uploadBarList),"paramType":"1"};
            // var fn="addNavScrollBar";
            // service = Commonjs.jsonToString(service);
            // var params = Commonjs.getParams(fn, encodeURIComponent(service));
            // Commonjs.ajaxSilence(weburl, params, true, function (response) {
            //     self.$message({
            //         message: '轮播图替换成功',
            //         type: 'success'
            //     });
            // }, function () {
            //     self.$message.error("获取导航配置错误");
            // });
        },
        updateBanner:function(){
            var self = this;
            var service = {"paramName":"navBarImg","paramValue": Commonjs.jsonToString(this.uploadBarList),"paramType":"1"};
            var fn="addNavScrollBar";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, encodeURIComponent(service));
            Commonjs.ajaxSilence(weburl, params, true, function (response) {
                self.$message({
                    message: '轮播图替换成功',
                    type: 'success'
                });
                self.isShow = false;
                self.$refs.uploader.clearFiles()
            }, function () {
                self.$message.error("获取导航配置错误");
            });
        },
        editBanner:function(data){
            this.isNew = false;
            this.isShow = true;
            this.bannerData = data;
            // bannerData:{
            //     "hrefs":'',
            //     "url": '',
            //     "name": '',
            //     "uid": '',

            // },
        },
        closeUpload:function(){
            this.isShow = false;
        },
        deleteBanner:function(data){
            var self = this;
            var list = [];
            self.uploadBarList.forEach((item, index) => {
                if (item.uid === data.uid) {
                    delete self.uploadBarList[index];
                }else{
                    list.push(item);
                }
            })
            self.uploadBarList = list;
            self.updateBanner();
        }

    },
    computed: {
        uploadDataDeal: function () {
            return {
                p: JSON.stringify({
                    uid: 1
                }),
                upname: ''
            };
        }
    },
    mounted:function () {
        this.getNavScrollData();
        // this.loadConfig();
        this.uploadUrl = realPath + '/upload.do';
    }
});
